package org.i0o.webplus.commons.util.populator.converters;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.i0o.webplus.commons.util.populator.exception.ConversionException;

/**
 * 日期转换类
 * 
 * @author <a href="mailto:781131@gmail.com">HTF</a>
 * @version 2010-5-29
 */
public class DateTimeConverter extends BaseConverter {

	/*
	 * (non-Javadoc)
	 * @see
	 * com.citinet.webplus.util.populator.converters.BaseConverter#convertToType
	 * (java.lang.Object, java.lang.Class, java.lang.Object[])
	 */
	@Override
	protected Object convertToType(Object value, Class<?> targetType, Object... configs) {
		// Timestamp
		if (value instanceof Timestamp) {
			Timestamp timestamp = (Timestamp) value;
			return toDate(targetType, timestamp.getTime());
		}

		// java.sql.Date
		if (value instanceof java.sql.Date) {
			java.sql.Date date = (java.sql.Date) value;
			return toDate(targetType, date.getTime());
		}

		// Date
		if (value instanceof Date) {
			Date date = (Date) value;
			return toDate(targetType, date.getTime());
		}

		// Calendar
		if (value instanceof Calendar) {
			Calendar date = (Calendar) value;
			return toDate(targetType, date.getTimeInMillis());
		}

		// Long
		if (value instanceof Long) {
			Long date = (Long) value;
			return toDate(targetType, date);
		}

		String stringValue = value.toString().trim();
		if (stringValue.length() == 0) {
			// TODO 如果字符串长度为0，则返回当前时间
			return null;
		} else {
			String strFormat = null;
			if (configs.length > 0) {
				strFormat = configs[0].toString();
			}
			return toDate(targetType, stringValue, strFormat);
		}
	}

	private Object toDate(Class<?> targetType, Long value) {
		if (targetType == Timestamp.class) {
			return new Timestamp(value);
		}
		
		if(targetType == java.sql.Date.class) {
			return new java.sql.Date(value);
		}
		
		if(targetType == Date.class) {
			return new Date(value);
		}
		
		if(targetType == Calendar.class) {
			
		}
		
		throw new ConversionException("Can't convert " + value + " to " + targetType.toString());
	}

	/**
	 * 将日期字符串转换成日期
	 * 
	 * @param targetType
	 *            目标类型
	 * @param value
	 *            日期字符串
	 * @param strFormat
	 *            字符串格式
	 * @return 转换后的类型
	 */
	private Object toDate(Class<?> targetType, String value, String strFormat) {

		if (strFormat == null || strFormat.trim().length() == 0) {
			String[] format = {
					"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy年MM月dd日"
			};
			Date date = null;
			for (String formatTmp : format) {
				SimpleDateFormat sf = new SimpleDateFormat(formatTmp);
				try {
					date = sf.parse(value);
					break;
				} catch (ParseException e) {
					log.debug("Can't use format \"" + formatTmp + "\" convert");
				}
			}

			if (date != null) {
				if(targetType == Date.class) {
					return date;
				}
				return toDate(targetType, date.getTime());
			}

			throw new ConversionException("Can't convert " + value + " to " + targetType.toString());
		} else {
			SimpleDateFormat sf = new SimpleDateFormat(strFormat);
			Date dateTmp = null;
			try {
				dateTmp = sf.parse(value);
			} catch (ParseException e) {
				log.error("ParseException", e);
				throw new ConversionException("Can't convert " + value + " to " + targetType.toString());
			}

			if (targetType == Date.class) {
				return dateTmp;
			}

			throw new ConversionException("Can't convert " + value + " to " + targetType.toString());
		}
	}

	@Override
	protected Class<?> getDefaultType() {
		return Date.class;
	}

}
